package com.hou.shop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hou.shop.product.dao.BrandDao;
import com.hou.shop.product.dao.CategoryDao;
import com.hou.shop.product.entity.BrandEntity;
import com.hou.shop.product.service.BrandService;
import com.hou.shop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hou.common.utils.PageUtils;
import com.hou.common.utils.Query;

import com.hou.shop.product.dao.CategoryBrandRelationDao;
import com.hou.shop.product.entity.CategoryBrandRelationEntity;
import com.hou.shop.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    BrandDao brandDao;
    @Autowired
    CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    // 保存分类与品牌之间的关系
    @Override
    public void saveBrandAndCateName(CategoryBrandRelationEntity categoryBrandRelation) {
        // 获取id字段
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        // 查询名称
        String brandName = brandDao.selectById(brandId).getName();
        String catelogName = categoryDao.selectById(catelogId).getName();

        // 将名称赋值给传入的参数
        categoryBrandRelation.setBrandName(brandName);
        categoryBrandRelation.setCatelogName(catelogName);

        // 保存数据
        this.save(categoryBrandRelation);

    }


    // 更新品牌名称
    @Override
    public void updateBrandName(Long brandId, String brandName) {

        // 构建table实体
        CategoryBrandRelationEntity table = new CategoryBrandRelationEntity();
        table.setBrandId(brandId);
        table.setBrandName(brandName);

        // 构建更新构造器
        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("brand_id", brandId);

        // 更新
        this.update(table,updateWrapper);

    }

    // 更新分组名称
    @Override
    public void updateCategoryName(Long catId, String categoryName) {
        // 构建table实体
        CategoryBrandRelationEntity table = new CategoryBrandRelationEntity();
        table.setBrandId(catId);
        table.setBrandName(categoryName);

        // 构建更新构造器
        UpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("catelog_id", catId);

        // 更新
        this.update(table,updateWrapper);
    }

    // 获取品牌名称
    @Override
    public List<BrandEntity> getBrandByCatId(Long catId) {

        List<CategoryBrandRelationEntity> brandRelationEntities = this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));


        List<BrandEntity> collect = brandRelationEntities.stream().map((obj) -> {
            Long brandId = obj.getBrandId();
            BrandEntity brandEntity = brandDao.selectById(brandId);
            return brandEntity;
        }).collect(Collectors.toList());


        return collect;
    }

}